#encoding:utf-8

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
import random
import time
# Create your models here.

def get_no():
    a='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    return timezone.now().strftime('%Y%m%d%H%M%S')+''.join(random.choice(a) for i in range(8))


class HookModel(models.Model):
    method=models.CharField('method',max_length=500,blank=True)
    path=models.TextField(verbose_name='url',blank=True)
    head=models.TextField(verbose_name='head',blank=True)
    body=models.TextField(verbose_name='body',blank=True)
    update_at = models.DateTimeField(auto_now=True)
    rsp=models.TextField(verbose_name='response content',blank=True)
    
    def __unicode__(self):
        return self.method


class CustomerModel(models.Model):
    user=models.OneToOneField(User,verbose_name='账号',null=True)
    nick_name=models.CharField('用户昵称',max_length=300,blank=True)
    mobile=models.CharField('手机号码',max_length=100,blank=True)
    head = models.CharField('头像',max_length=300,blank=True)
    vcode=models.CharField('邀请码',max_length=300,blank=True) 
    openid=models.CharField('微信openid',max_length=300,blank=True) 
    
    def __unicode__(self):
        return self.nick_name
    
    @classmethod
    def get_or_add(cls,**kw):
        """
        CustomerModel.get_or_add(**kw)
        如果有对应customer，则返回，
        如果没有，则创建custormer，此外创建对应的user
        """
        obj,is_created=CustomerModel.objects.get_or_create(**kw)
        if is_created:
            user =User.objects.create_user(username=str(int(time.time()*100)))
            obj.user=user
            obj.save()
        return obj
            
    class Meta:
        verbose_name='客户信息'
        verbose_name_plural='客户信息'

class UserCarModel(models.Model):
    user = models.ForeignKey(User,verbose_name='账号',null=True,blank=True)
    car_no= models.CharField('车牌号码',max_length=50,blank=True)
    car_brand=models.ForeignKey('CarBrandModel',verbose_name='品牌',null=True,blank=True)
    #models.CharField('车辆品牌',max_length=300,blank=True)
    car_model=models.ForeignKey('CarModelModel',verbose_name='型号',null=True,blank=True)
    #models.CharField('车辆型号',max_length=300,blank=True)
    car_buydate=models.CharField('购买日期',max_length=100,blank=True)
    
    def __unicode__(self):
        return self.car_no
    
    class Meta:
        verbose_name='客户汽车信息'
        verbose_name_plural='客户汽车信息'        

class CarWashLocalModel(models.Model):
    no=models.CharField('网点编号',max_length=100,blank=True)
    name = models.CharField('名称',max_length=100,blank=True)
    wifi_ssid=models.CharField('wifi ID',max_length=100,blank=True)
    wifi_passwd=models.CharField('wifi 密码',max_length=100,blank=True)
    status=models.CharField('网点状态',max_length=100,blank=True)
    jd = models.CharField('经度',max_length=100,blank=True)
    wd = models.CharField('维度',max_length=100,blank=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name='洗车点'
        verbose_name_plural='洗车点'      

class MealModel(models.Model):
    name=models.CharField('标示名',max_length=100,blank=True,unique=True)
    label=models.CharField('名字',max_length=100,blank=True)
    org_price=models.CharField('原价',max_length=100,blank=True)
    price=models.CharField('价格',max_length=100,blank=True)
    detail=models.TextField(verbose_name='详细描述',blank=True)
    status=models.CharField('状态',max_length=100,blank=True)
    priority=models.CharField('优先级',max_length=100,blank=True)
    
    def __unicode__(self):
        return self.label

ORDER_STATUS=(
    ('active','Active'),
    ('used','Used'),
    ('expired','Expired'),
    ('deactive','Deactive')
)
class OrderModel(models.Model):
    meal=models.ForeignKey(MealModel,verbose_name='套餐',blank=True,null=True)
    car=models.ForeignKey(UserCarModel,verbose_name='汽车',blank=True,null=True)
    no = models.CharField('订单号',max_length=300,blank=True)
    status=models.CharField('状态',max_length=100,blank=True,choices=ORDER_STATUS)
    start_time=models.CharField('起始日期',max_length=100,blank=True)
    end_time=models.CharField('截止日期',max_length=100,blank=True)
    
    def __init__(self,*args,**kw):
        super(OrderModel,self).__init__(*args,**kw)
        if not self.no:
            self.no= get_no()   
    
    def last_session(self):
        query =WashSessionModel.objects.filter(order=self).order_by('-id')
        if query.exists():
            return query[0]
        

class WXOrderModel(models.Model):
    order=models.OneToOneField(OrderModel,verbose_name='订单',blank=True,null=True)
    meal=models.ForeignKey(MealModel,verbose_name='套餐',blank=True,null=True)
    coupon=models.ForeignKey('CouponModel',verbose_name='优惠券',blank=True,null=True)
    org_fee=models.CharField('原价',max_length=100,blank=True)
    no = models.CharField('内部微信订单号',max_length=300,blank=True)
    transaction_id=models.CharField('微信支付订单号',max_length=300,blank=True)
    time_end=models.CharField('支付完成时间',max_length=300,blank=True)
    total_fee=models.CharField('总金额',max_length=300,blank=True)
    openid=models.CharField('付款人openid',max_length=300,blank=True)
    trade_type=models.CharField('交易类型',max_length=300,blank=True)
    result_code=models.CharField('业务结果',max_length=300,blank=True)
    create_time=models.DateTimeField(verbose_name='记录创建时间',auto_now_add=True,null=True)
    last_update_time=models.DateTimeField(verbose_name='记录最后修改时间',auto_now=True,null=True)
    pay=models.CharField('支付情况',max_length=100,blank=True)
    
    car=models.ForeignKey(UserCarModel,verbose_name='汽车',blank=True,null=True)

    
    def __init__(self,*args,**kw):
        super(WXOrderModel,self).__init__(*args,**kw)
        if not self.no:
            self.no= 'WX'+get_no()

class CouponModel(models.Model):
    no=models.CharField('优惠券号',max_length=300,blank=True)
    name=models.CharField('标示名',max_length=100,blank=True)
    label=models.CharField('名字',max_length=100,blank=True) 
    value=models.CharField('优惠额度',max_length=100,blank=True) 
    status=models.CharField('使用状态',max_length=100,blank=True) 
    
    def __init__(self,*args,**kw):
        super(WXOrderModel,self).__init__(*args,**kw)
        if not self.no:
            self.no= get_no()    
    
    
    
class WashSessionModel(models.Model):
    zk=models.ForeignKey(CarWashLocalModel,verbose_name='网点',blank=True,null=True)
    order=models.ForeignKey(OrderModel,verbose_name='所用订单',blank=True,null=True)
    car = models.ForeignKey(UserCarModel,verbose_name='汽车',blank=True,null=True)
    
    # start = models.DateTimeField(verbose_name='开始时间',blank=True,null=True)
    # end=models.DateTimeField(verbose_name='结束时间',blank=True,null=True)
    
    class Meta:
        verbose_name='洗车过程'
        verbose_name_plural='洗车过程'    


class CarBrandModel(models.Model):
    parent=models.ForeignKey('self',verbose_name='父品牌',blank=True,null=True)
    name=models.CharField('品牌名称',max_length=200,blank=True)
    jp=models.CharField('品牌简拼',max_length=100,blank=True)
    qp=models.CharField('品牌全拼',max_length=100,blank=True)
    logo=models.CharField('品牌标志',max_length=300,blank=True)
    cls=models.CharField('等级',max_length=50,blank=True)
    kind=models.CharField('品牌类别',max_length=50,blank=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name='车辆品牌'
        verbose_name_plural='车辆品牌'     
    
    #brand_no = NotNullColumn(VARCHAR(50),doc='品牌编号')
    #brand_parentno = NotNullColumn(VARCHAR(50),doc='父类编号')
    #brand_name = NotNullColumn(VARCHAR(50), doc='车辆品牌名称')
    #brand_jp = NotNullColumn(VARCHAR(50),doc='车辆品牌简拼')
    #brand_qp = NotNullColumn(VARCHAR(50),doc='车辆品牌全拼')
    #brand_logo =  NotNullColumn(VARCHAR(256),doc='品牌logo')
    #brand_class =  NotNullColumn(INTEGER,server_default='0',doc='')
    #brand_type = NotNullColumn(VARCHAR(16),doc='品牌类型 mb，cb')


class CarModelModel(models.Model):
    brand=models.ForeignKey(CarBrandModel,verbose_name='品牌',blank=True,null=True)
    name=models.CharField('型号名称',max_length=200,blank=True)
    jp=models.CharField('型号简拼',max_length=100,blank=True)
    qp=models.CharField('型号全拼',max_length=100,blank=True)
    img=models.CharField('型号图片',max_length=300,blank=True) 
    min_price=models.CharField('最低价格',max_length=100,blank=True)
    max_price=models.CharField('最高价格',max_length=100,blank=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name='车辆型号'
        verbose_name_plural='车辆型号'     
    
    #__tablename__ = 'car_model'
    #brand_no = NotNullColumn(VARCHAR(50),doc='品牌编号')
    #model_no = NotNullColumn(VARCHAR(50),doc='型号编号')
    #model_name = NotNullColumn(VARCHAR(50),doc='型号名称')
    #model_jp = NotNullColumn(VARCHAR(50),doc='型号简拼')
    #model_qp = NotNullColumn(VARCHAR(50),doc='型号全拼')
    #model_pic = NotNullColumn(VARCHAR(256),doc='型号图片')
    #model_minprice = NotNullColumn(VARCHAR(10),doc='型号最低价钱')
    #model_maxprice = NotNullColumn(VARCHAR(10),doc='型号最高价钱')


class ZKWashLog(models.Model):
    zk=models.ForeignKey(CarWashLocalModel,verbose_name='中控场地',blank=True,null=True)
    session=models.ForeignKey(WashSessionModel,verbose_name='洗车会话',blank=True,null=True)
    code=models.CharField('状态码',max_length=50,blank=True)
    update_at=models.DateTimeField(verbose_name='更新时间',auto_now_add=True,blank=True,null=True)    

class ZKOtherLog(models.Model):
    zk= models.ForeignKey(CarWashLocalModel,verbose_name='中控场地',blank=True,null=True)
    code=models.CharField('错误代码',max_length=50,blank=True)
    update_at=models.DateTimeField(verbose_name='更新时间',auto_now_add=True,blank=True,null=True)

